package com.wangshuqi.spring.dao.impl;

import com.wangshuqi.spring.dao.UserDAO;
import com.wangshuqi.spring.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDAOImpl implements UserDAO {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public int add(User user) {
        String sql = "insert into user(name,password,address,phone) values(?,?,?,?)";
        int insert = jdbcTemplate.update(sql, user.getName(), user.getPassword(), user.getAddress(), user.getPhone());
        return insert;
    }

    @Override
    public int update(User user) {
        String sql = "update user set name=?,password=? where id=?";
        int update = jdbcTemplate.update(sql, user.getName(), user.getPassword(), user.getId());
        return update;
    }

    @Override
    public int deleteById(Integer id) {
        String sql = "delete from user where id = ?";
        int delete = jdbcTemplate.update(sql, id);
        return delete;
    }


    @Override
    public User selectById(Integer id) {
        String sql = "select * from user where id = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);
        return user;
    }

    @Override
    public List<User> selectByName(String name) {
        String sql = "select * from user where name like concat('%',?,'%')";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class), name);
        return list;
    }

    @Override
    public Integer selectCountByName(String name) {
        String sql = "select count(*) from user where name like concat('%',?,'%')";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class, name);
        return count;
    }

    @Override
    public int[] batchInsert(List<User> userList) {
        String sql = "insert into user(name,password,address,phone) values(?,?,?,?)";
        List<Object[]> batchArgs = new ArrayList<>();
        for (User user : userList) {
            Object[] args = {user.getName(), user.getPassword(), user.getAddress(), user.getPhone()};
            batchArgs.add(args);
        }
        int[] inserts = jdbcTemplate.batchUpdate(sql, batchArgs);
        return inserts;
    }

    @Override
    public int[] batchUpdate(List<User> userList) {
        String sql = "update user set name=?,password=? where id=?";
        List<Object[]> batchArgs = new ArrayList<>();
        for (User user : userList) {
            Object[] args = {user.getName(), user.getPassword(), user.getId()};
            batchArgs.add(args);
        }
        int[] updates = jdbcTemplate.batchUpdate(sql, batchArgs);
        return updates;
    }

    @Override
    public int[] batchDelete(List<Integer> idList) {
        String sql = "delete from user where id = ?";
        List<Object[]> batchArgs = new ArrayList<>();
        for (Integer id : idList) {
            Object[] args = {id};
            batchArgs.add(args);
        }
        int[] deletes = jdbcTemplate.batchUpdate(sql, batchArgs);
        return deletes;
    }
}
